﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EnvDTE;

namespace Ramp.VSAddIn.ToolsOptions
{
    public partial class OptionsPage : UserControl, IDTToolsOptionsPage
    {
        #region Constructor
        
        public OptionsPage()
        {
            InitializeComponent();
        }

        #endregion Constructor

        # region IDTToolsOptionsPage Members

        public void GetProperties(ref object PropertiesObject)
        {
            PropertiesObject = null;
        }

        public void OnAfterCreated(DTE DTEObject)
        {
            Settings.Load();

            if (string.IsNullOrWhiteSpace(Settings.ServiceHostName))
            {
                hostNameTextBox.Text = "localhost";
            }
            else
            {
                hostNameTextBox.Text = (string)Settings.ServiceHostName;
            }

            if (Settings.ServicePortNumber >= 1025 && Settings.ServicePortNumber <= 99999)
            {
                portNumericUpDown.Value = (decimal)Settings.ServicePortNumber;
            }
            else
            {
                portNumericUpDown.Value = 9898;
            }
        }

        /// <summary>
        /// Tools|Options dialog Cancel button's pressed.
        /// </summary>
        public void OnCancel()
        {
        }

        public void OnHelp()
        {
        }

        /// <summary>
        /// Tools|Options dialog OK button's pressed.
        /// </summary>
        public void OnOK()
        {
            //System.Diagnostics.Debug.WriteLine("Ramper AddIn: OptionsPage: In OnOK");            
            Settings.ServiceHostName = hostNameTextBox.Text;
            //System.Diagnostics.Debug.WriteLine("Ramper AddIn: OptionsPage: ServiceHostName : " + hostNameTextBox.Text);

            if (string.IsNullOrWhiteSpace(Settings.ServiceHostName))
            {
                //System.Diagnostics.Debug.WriteLine("Ramper AddIn: OptionsPage: Setting ServiceHostName to localhost");
                Settings.ServiceHostName = "localhost";
            }

            Settings.ServicePortNumber = (int)portNumericUpDown.Value;
            //System.Diagnostics.Debug.WriteLine("Ramper AddIn: OptionsPage: ServicePortNumber : " + portNumericUpDown.Value);

            if (!(Settings.ServicePortNumber >= 1025 && Settings.ServicePortNumber <= 99999))
            {
                //System.Diagnostics.Debug.WriteLine("Ramper AddIn: OptionsPage: Setting ServicePortNumber to 9898");
                Settings.ServicePortNumber = 9898;
            }

            //System.Diagnostics.Debug.WriteLine("Ramper AddIn: OptionsPage: Calling Settings.Save()");
            Settings.Save();
        }

        #endregion

        private void restoreDefaults_Click(object sender, EventArgs e)
        {
            Settings.ServiceHostName = "localhost";
            Settings.ServicePortNumber = 9898;
            hostNameTextBox.Text = Settings.ServiceHostName;
            portNumericUpDown.Value = Settings.ServicePortNumber;
            Settings.Save();
            Settings.Load();
        }
    }
}
